package com.espirit.common;

import java.io.File;

public class TxFile {
	
	private String fullFilePropertyName;
	private String destPicFileName;
	
	private File from;
	
	private File to;
	
	private boolean enableAutoCommit;
	
	public TxFile(File from, File to, String fullFilePropertyName, String destPicFileName) {
		this.from = from;
		this.to = to;
		this.enableAutoCommit = true;
		this.fullFilePropertyName = fullFilePropertyName;
		this.destPicFileName = destPicFileName;
	}
	
	public boolean isEnableAutoCommit() {
		return enableAutoCommit;
	}

	public void setEnableAutoCommit(boolean enableAutoCommit) {
		this.enableAutoCommit = enableAutoCommit;
	}

	public void comit() {
		if (from.exists()) {		// getId() != null && 
			to.getParentFile().mkdirs();
			
			/*
			if (model instanceof EncryptedFile) {		// see StatGenericJPAService.viewFile
				try {
					// TODO: each file has their own encrypted seed?
					String SEED = "0966584141573219732317534";
					byte[] bytes = FileUtils.readFileToByteArray(from);
					BigInteger bi_text = new BigInteger(bytes);
					BigInteger bi_r0 = new BigInteger(SEED);
					BigInteger bi_r1 = bi_r0.xor(bi_text);
					FileUtils.writeByteArrayToFile(to, bi_r1.toByteArray());
				} catch (Exception ex) {
					logger.error("Failed to encrypt the file", ex);
				}
			}*/
			
			from.renameTo(to);
		}
	}

	public void setFrom(File from) {
		this.from = from;
	}

	public File getFrom() {
		return from;
	}

	public File getTo() {
		return to;
	}

	public String getFullFilePropertyName() {
		return fullFilePropertyName;
	}

	public String getDestPicFileName() {
		return destPicFileName;
	}
}
